import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.sql.*;
import java.util.Locale;

/**
 * Created by zy on 2019/3/28.
 */
public class add_book {
    private JButton commit;
    //Alert alert = new Alert();

    private JPanel panel;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;

    public static void main(String arg[]) {
        JFrame frame = new JFrame("czy");
        JPanel panel = new JPanel();
        panel.add(new add_book().panel);
        //JFileChooser chooser = new JFileChooser();
        //panel.add(chooser);
        JTextArea area = new JTextArea();
        panel.add(area);
        frame.setContentPane(panel);
        frame.setBounds(100, 100, 600, 400);
        frame.setResizable(false);
        //frame.setSize(2000,1800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.pack();
        frame.setVisible(true);
    }

    public add_book() {
        commit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                JFrame frame = new JFrame("choose");
                frame.setContentPane(new JFileChooser());
                frame.setBounds(100, 100, 600, 400);
                frame.setResizable(false);
                frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
                frame.setVisible(true);
                Connection conn = null;
                String sql;
                // MySQL的JDBC URL编写方式：jdbc:mysql://主机名称：连接端口/数据库的名称?参数=值
                // 避免中文乱码要指定useUnicode和characterEncoding
                // 执行数据库操作之前要在数据库管理系统上创建一个数据库，名字自己定，
                // 下面语句之前就要先创建javademo数据库
                String url = "jdbc:mysql://localhost:3306/library?"
                        + "user=root&password=android&useUnicode=true&characterEncoding=UTF-8";

                try {
                    // 之所以要使用下面这条语句，是因为要使用MySQL的驱动，所以我们要把它驱动起来，
                    // 可以通过Class.forName把它加载进去，也可以通过初始化来驱动起来，下面三种形式都可以
                    Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动
                    // or:
                    // com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
                    // or：
                    // new com.mysql.jdbc.Driver();

                    System.out.println("成功加载MySQL驱动程序");
                    // 一个Connection代表一个数据库连接
                    conn = DriverManager.getConnection(url);
                    // Statement里面带有很多方法，比如executeUpdate可以实现插入，更新和删除等
                    Statement stmt = conn.createStatement();
                    String name = textField3.getText();
                    String author = textField2.getText();
                    String nation = textField1.getText();
                    String format = "'%s','%s','%s'";
                    String value = String.format(format, name, author, nation);
                    //ResultSet rs = stmt.executeQuery("SELECT * FROM library.books WHERE name!='" + name + "';");
                    //System.out.println(String.format("%-12s%-6s%s", "书名", "国籍", "作者").replaceAll(" ", "　"));
                    //System.out.println();
                    //while (rs.next()) {
                    //    System.out
                    //            .println(String.format("%-12s%-6s%s", rs.getString(2), rs.getString(4), rs.getString(3)).replaceAll(" ", "　"));// 入如果返回的是int类型可以用getInt()
                    //}
                    sql = "INSERT INTO library.books(name, author, nation) VALUE (" + value + ");";
                    //int result = stmt.executeUpdate(sql);// executeUpdate语句会返回一个受影响的行数，如果返回-1就没有成功
                    conn.close();
                } catch (SQLException e) {
                    System.out.println("MySQL操作错误");
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                }
            }
        });
        panel.addComponentListener(new ComponentAdapter() {
        });
    }

}
/*子页面*/

class Alert {
    private JTextArea textArea1;
    JFrame frame;
    Alert alert;

    Alert() {
        if (frame == null) {
            frame = new JFrame("Alert");
        }
    }

    public void build(String s) {
        if (alert == null) {
            alert = new Alert();
            panel.updateUI();
            textArea1.append(s);
            frame.setContentPane(alert.panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setBounds(200, 200, 200, 150);
            frame.pack();
            frame.setVisible(true);
        }

    }

    private JPanel panel;

}
